package com.cutebaby.admin.com.mybatis;

import com.cutebaby.admin.com.mybatis.dao.UserMapper;
import com.cutebaby.admin.com.mybatis.entity.A;
import com.cutebaby.admin.com.mybatis.entity.User;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author: xialegemo
 * @description:
 * @date： 2020/10/26 22:26
 **/
public class  Test {


    public static void main(String[] args){
        // 注解方式启动
        AnnotationConfigApplicationContext context
                = new AnnotationConfigApplicationContext(AppConfig.class);


        // 对于xml方式启动，需要传入一个xml的配置文件，xml中需要定义bean的位置
//        ClassPathXmlApplicationContext


        // bean和对象的概念：
        // 可以直接从spring容器里获取到名称为user的bean
        // 根据user这个name从applicationContext里获取类型为User.class的Bean，
        // 但在被TestBeanFactoryPostProcessor的BeanDefinition执行setBeanClass后，该部分报错
        User user = context.getBean("user", User.class);   //从spring容器里获取到的 会给对象里的属性赋值

        User user1 = new User(); // 手动new   里面的肯定没有a
        System.out.println("user内存地址: " +  user);
        System.out.println("user.getA(): " +  user.getA());
        System.out.println("user1内存地址: " +  user);
        System.out.println("user1.getA(): " +  user1.getA());


//        UserMapper userMapper = (UserMapper) context.getBean("user");
//        System.out.println(userMapper);


        System.out.println(context.getBean("luBanFactoryBean"));
        /*
        * 当get Object()中为  return new B(); 时，此处打印结果为com.cutebaby.admin.com.mybatis.entity.B@e3b3b2f
        * 也就是最终getBean("luBanFactoryBean")  得到的是get Object()里面的对象
        * */

    }

    /*
    * 当使用BeanDefinition对bean的信息进行修改时，修改见 TestBeanFactoryPostProcessor，会有如下报错 ：
    * 提示 user需要User这个类，但是得到的是A这个类 ，说明TestBeanFactoryPostProcessor 的setBeanClass确实生效了
    *
    * Exception in thread "main" org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'user' is expected to be of type 'com.cutebaby.admin.com.mybatis.entity.User' but was actually of type 'com.cutebaby.admin.com.mybatis.entity.A'
	* at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:392)
	* at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)
	* at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1111)
	* at com.cutebaby.admin.com.mybatis.Test.main(Test.java:26)
    * ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2
    * JDWP exit error AGENT_ERROR_NO_JNI_ENV(183):  [util.c:840]

    *
    *
    * */
}
